package br.com.medical.shop.web.controladores.sistema.uc001;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.com.medical.shop.negocio.administrativo.Usuario;
import br.com.medical.shop.web.controladores.servico.RomService;
import br.com.medical.shop.web.controladores.servico.UserService;


@Controller
public class OpemMeetings {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RomService roomService;
	
	private int counterUsers = 1;
	
	// URL inicial
	@RequestMapping(value = "/" , method = RequestMethod.GET)
	public String inicio( ModelMap modelo){
		return "001";
	}
	
	@RequestMapping(value = "/login" , method = RequestMethod.POST)
	public String login( ModelMap modelo, HttpServletRequest request){
		
		Usuario usuario = new Usuario();
		usuario.setId(counterUsers);
		usuario.setApelido(request.getParameter("apelido"));
		usuario.setCpf(request.getParameter("cpf"));
		usuario.setEmail(request.getParameter("email"));
		usuario.setNome(request.getParameter("nome"));
		
		counterUsers++;
		
		request.getSession().setAttribute("user", usuario);
		return "redirect:"+roomService.URLBASE+"shop/salas.htm";
	}
	
	@RequestMapping(value = "/manter_sala" , method = RequestMethod.GET)
	public String cadastroGet( ModelMap modelo, HttpServletRequest request){
		return "004";
	}
	
	@RequestMapping(value = "/manter_sala" , method = RequestMethod.POST)
	public String cadastro( ModelMap modelo, HttpServletRequest request){
		String retorno = roomService.addRoom(request);
		modelo.put("retorno", retorno);
		return Integer.parseInt(retorno) > 0 ? "redirect:"+roomService.URLBASE+"shop/salas.htm" : "004";
	}

	@RequestMapping(value = "/salas" , method = RequestMethod.GET)
	public String salasGet( ModelMap modelo, HttpServletRequest request){
		modelo.put("rooms",roomService.getRooms(request));
		return "003";
	}
	
	@RequestMapping(value = "/sala" , method = RequestMethod.GET)
	public String acessarSala( ModelMap modelo, HttpServletRequest request){
		Usuario usuario = (Usuario) request.getSession().getAttribute("user");
		String hash = roomService.getRoomUrl(usuario, request);
		return "redirect:"+hash;
	}
}
